package com.qijian.flowable.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.enums.BusinessType;
import com.qijian.flowable.domain.FlowableRule;
import com.qijian.flowable.domain.query.FlowableRuleQuery;
import com.qijian.flowable.service.IFlowableRuleService;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.common.core.page.TableDataInfo;

/**
 * 流程流程规则Controller
 *
 * @author qijian
 * @date 2021-09-17
 */
@Api(tags = "流程模块：流程流程规则")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowable/flowableRule")
public class FlowableRuleController extends BaseController {

    private final IFlowableRuleService flowableRuleService;

    /**
     * 查询流程流程规则列表
     */
    @GetMapping("/list")
        public TableDataInfo list(FlowableRuleQuery query, IPage<FlowableRule> page) {
        return getDataTable(flowableRuleService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }


    /**
     * 查询流程流程规则列表
     */
    @GetMapping("/listNew")
    public TableDataInfo listNew(FlowableRuleQuery query) {
        return getDataTable(flowableRuleService.list(WhereEntityTool.invoke(query)));
    }

    /**
     * 导出流程流程规则列表
     */
    @Log(title = "流程流程规则" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FlowableRuleQuery query) {
        return new ExcelUtil<>(FlowableRule. class).exportExcel(flowableRuleService.list( WhereEntityTool.invoke(query)), "流程流程规则数据");
    }

    /**
     * 获取流程流程规则详细信息
     */
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") Long ruleId) {
        return AjaxResult.success(flowableRuleService.getById(ruleId));
    }

    /**
     * 新增流程流程规则
     */
    @Log(title = "流程流程规则" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlowableRule flowableRule) {
        return toAjax(flowableRuleService.save(flowableRule));
    }

    /**
     * 修改流程流程规则
     */
    @Log(title = "流程流程规则" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlowableRule flowableRule) {
        return toAjax(flowableRuleService.updateById(flowableRule));
    }

    /**
     * 删除流程流程规则
     */
    @Log(title = "流程流程规则" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(flowableRuleService.removeByIds(ids));
    }
}
